<template>
  <div>
    <!-- 发票邮寄下发 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhMailSend"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :disabled="!isCan" :click="dataHandle.bind(this, 1)">下 发</multiuse-button>
        <multiuse-button size="small" type="primary" :disabled="!isCan" :click="dataHandle.bind(this, 2)">取 消</multiuse-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <SonModel :type="handleType" :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { queryList, mailIssued, mailCancel } from '@/api/omFinance/InvoiceMailManage/MailSend';
import SonModel from './SonModel';

export default {
  name: 'MailSend',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    this.conf = { show: '下发' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {
        postType: 60901001,
        mailYear: moment().format('YYYY'),
        mailMonth: moment().format('MM')
      },
      formData: {},
      visible: false,
      handleType: '',
      drawerTitle: '',
      isCan: false,
      ids: []
    };
  },
  components: {
    SonModel
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    changeHandle(params) {
      params.mailYear = params.mailYearAndMonth.split('-')[0];
      params.mailMonth = params.mailYearAndMonth.split('-')[1];
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    resetChange(params) {
      params.postType = 60901001;
      params.mailYear = params.mailYearAndMonth.split('-')[0];
      params.mailMonth = params.mailYearAndMonth.split('-')[1];
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    emitcloseHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    selectChangeHandle(row) {
      this.ids = [];
      if (row.length > 0) {
        this.isCan = true;
        row.map(x => {
          this.ids.push(x.id);
        });
        return;
      }
      this.isCan = false;
    },
    async dataHandle(type) {
      let str = type == 1 ? '下发' : '取消';
      await this.$confirm(`是否${str}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let res = null;
          if (type == 1) {
            res = await mailIssued(this.ids);
          } else {
            res = await mailCancel(this.ids);
          }
          if (res.resultCode == 200) {
            this.$notify.success({ title: '提示信息', message: `${str}成功！` });

            this.fetchParams = { ...this.fetchParams };
          }
        })
        .catch(() => {});
    },
    emitcloseHandler() {
      this.visible = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },

    createdFrom() {
      return [
        {
          type: 'DATE',
          label: '邮寄单年月',
          fieldName: 'mailYearAndMonth',
          initialValue: moment().format('YYYY-MM'),
          dateType: 'month',
          clearable: false
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'SELECT',
          label: '业务类型',
          fieldName: 'businessType',
          placeholder: '请选择',
          itemList: this.createDictList('6089')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 60,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  下发
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮寄单号',
          dataIndex: 'mailNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '业务类型',
          dataIndex: 'businessType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6089'),
          render: props => {
            return <span>{this.createDictText(props.row.businessType, '6089')}</span>;
          }
        },
        {
          title: '邮寄单年月',
          dataIndex: 'mailYearAndMonth',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    }
  }
};
</script>
